import java.util.Comparator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        int[] nums={3,2,1,5,6,4};
        System.out.println(main.findKthLargest(nums, 2));
    }

    // 只出现一次的数字
    public int singleNumber(int[] nums) {
        int result = 0;
        for (int num : nums) {
            result ^= num;
        }
        return result;
    }

    //数组中的第K个最大元素
    //堆的建立是小根堆
    class Intcmp implements Comparator<Integer> {
        @Override
        public int compare(Integer i1, Integer i2) {
            return i1-i2;
        }
    }

    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>(new Intcmp());
        for (int i=0; i<k;i++){
            pq.offer(nums[i]);
        }
        for(int i=k; i<nums.length; i++){
            if(pq.peek()<nums[i]){
                pq.poll();
                pq.offer(nums[i]);
            }else{
                continue;
            }
        }
        return pq.peek();
    }
}